﻿/**
 * jQuery EasyUI 1.3.5 Copyright (c) 2009-2013 www.jeasyui.com. All rights
 * reserved.
 * 
 * Licensed under the GPL or commercial licenses To use it on other terms please
 * contact author: info@jeasyui.com http://www.gnu.org/licenses/gpl.txt
 * http://www.jeasyui.com/license_commercial.php
 * 
 * jQuery EasyUI importor Extensions 1.0 beta jQuery EasyUI import 组件扩展
 * jeasyui.extensions.excelimport.js 二次开发 何庆明 最近更新：2013-09-29
 * 
 * 依赖项： 1、jquery.jdirk.js v1.0 beta late 2、jeasyui.extensions.js v1.0 beta late
 * 3、jeasyui.extensions.menu.js v1.0 beta late 4、jeasyui.extensions.panel.js
 * v1.0 beta late 5、jeasyui.extensions.window.js v1.0 beta late
 * 6、jeasyui.extensions.dialog.js v1.0 beta late 7、jeasyui.extensions.toolbar.js
 * v1.0 beta late 8、icons/jeasyui.icons.all.js 和 icons/icon-all.css v1.0 beta
 * late
 * 
 * Copyright (c) 2013 ChenJianwei personal All rights reserved.
 * http://www.chenjianwei.org
 */
(function($, undefined) {

	$.util.namespace("$.easyui.importor");

	// 增加自定义扩展方法 $.easyui.icons.showImportor；该方法弹出一个图标选择框窗口；该方法定义如下参数：
	// diaOptions: 这是一个 JSON-Object 对象；具体格式参考 $.easyui.showDialog 方法的参数 diaOptions
	// 的格式；
	// 该参数格式在 $.easyui.showDialog 参数 diaOptions 格式基础上扩展了如下属性：
	// onEnter:
	// onExportSuccess:
	// 返回值：返回弹出窗口的 easyui-dialog 控件对象(jQuery-DOM 格式)。
	$.easyui.importor.showImportor = function(uploadifyOptions) {
		if (uploadifyOptions && $ != $.util.$) {
			return $.util.$.easyui.importor.showImportor.apply(this, arguments);
		}
		if(!uploadifyOptions.importNo){
			alert("请配置导入模板编号");
		}
		var opts = {
			width: 350,
			minWidth: 350,
			height: 250,
			minHeight: 250,
			title: "上传导入文件",
			iconCls: "icon-standard-paintcan",
			maximizable: true,
			collapsible: true,
			attachmentIds: "",
			onEnter: function(value) {}
		};
		var upOpts = $.extend({
			'height' : 20,
			'width' : 60,
			'buttonText' : '上传文件',
			'queueID' : 'queueDiv',
			'auto' : true,
			'fileTypeExts' : '*.xls;*.xlsx',
			'queueSizeLimit' : 1,// 上传队列中所允许的文件数量
			'simUploadLimit' : 1,// 允许同时上传的文件数量
			'sizeLimit' : 102400,// 单个上传文件的大小限制，单位为字节
			'swf' : kl.ctx + '/static/uploadify/uploadify.swf',
			'uploader' : kl.ctx + '/basic/excelImportFile/upload;JSESSIONID='+kl.jsessionid,
			'downloader' : kl.ctx+'/basic/excelImport/excelTemplet/'+uploadifyOptions.importNo,
			'onUploadStart' : function(file) {
			},
			'onUploadSuccess' : function(file, data, response) {
				$.easyui.loading({
					msg : "Excel上传成功，正在解析中……"
						, locale: ".importorClass"});
				var data = eval('(' + data + ')');
				$.get(kl.ctx+"/basic/excelImport/excelImport/"+uploadifyOptions.importNo+"/" + data.obj, function(result) {
					$.easyui.loaded(".importorClass");
					showInfoForCallBack(result);
					if (result.success) {
						uploadifyOptions.onExportSuccess.call(this, file, data);
					}
				}, 'json');
			},
			'onUploadComplete' : function() {
				$('#importLispDialog').window('close');
			}
		},
		uploadifyOptions);
		var attachmentIds = opts.attachmentIds,
		dia = $.easyui.showDialog($.extend({},
		opts, {
			content: "<div class=\"importorClass\"><a id=\"file_upload\" ></a><a id=\"file_download\" ></a><div id=\"queueDiv\" class=\"file_queue\"></div><div style=\"color:red\">导入Excel格式请通过下方的‘模板下载’查看</div></div>",
			saveButtonText: "确定",
			saveButtonIconCls: "icon-ok",
			enableApplyButton: false,
			topMost: true,
			onSave: function() {
				if ($.isFunction(opts.onEnter)) {
					return opts.onEnter.call(this, attachmentIds);
				}
			},buttons:[{
				iconCls: 'icon-standard-table',
			    text : '模板下载',
				handler:function(){
					window.parent.location.href=upOpts.downloader;
				}
			}]
		}));
		$.util.exec(function() {
			dia.find("#file_upload").uploadify(upOpts);
		});
		return dia;
	};

	var css = ".file_queue{background-color:#FFF;border-radius:3px;box-shadow:0 1px 3px rgba(0,0,0,0.25);height:103px;margin-bottom:10px;overflow:auto;padding:5px 10px;width:300px}";
	$.util.addCss(css);

})(jQuery);